package com.example.xianji.activity;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;

import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class Main_YiJian_FanKuiActivity extends Activity implements OnClickListener {
    private RelativeLayout suggestion_back;
    private EditText yijian_content, yijian_details;
    private Button suggestion_tijiao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__yi_jian__fan_kui);
        //XJApplication.getInstance().addActivity(this);
        suggestion_back = (RelativeLayout) findViewById(R.id.suggestion_relative_back);
        yijian_content = (EditText) findViewById(R.id.yijian_content);
        yijian_details = (EditText) findViewById(R.id.yijian_details);
        suggestion_tijiao = (Button) findViewById(R.id.suggestion_tijiao);

        suggestion_back.setOnClickListener(this);
        suggestion_tijiao.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.suggestion_relative_back) {
            this.finish();
        } else if (v.getId() == R.id.suggestion_tijiao) {
            if (yijian_content.getText().length() > 0) {
                if (yijian_details.getText().toString().length() > 0) {
                    yijian_data();
                } else {
                    Toast.makeText(Main_YiJian_FanKuiActivity.this, "请填写反馈内容", 0).show();
                }
            } else {
                Toast.makeText(Main_YiJian_FanKuiActivity.this, "请填写联系方式", 0).show();
            }
        }
    }

    public void yijian_data() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("contact", yijian_content.getText().toString());
        map.put("details", yijian_details.getText().toString());
        JSONObject parmas = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST, Contant.yijian,
                parmas, new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getInt("status") == 1) {
                        Toast.makeText(Main_YiJian_FanKuiActivity.this, response.getString("msg"), 0).show();
                        Main_YiJian_FanKuiActivity.this.finish();
                    } else {
                        Toast.makeText(Main_YiJian_FanKuiActivity.this, response.getString("msg"), 0).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();
    }
}
